package com.cheng.student.service;

import com.cheng.student.beans.AmountAndListStu;
import com.cheng.student.beans.AmountAndListTeach;
import com.cheng.student.beans.Teacher;
import com.cheng.student.dao.TeacherDao;
import com.cheng.student.dao.mappers.TeacherMapper;
import com.cheng.student.service.interfaces.TeacherService;
import com.cheng.student.utils.GetJsonUtil;
import com.cheng.student.utils.SqlSessionGetter;
import org.apache.ibatis.session.SqlSession;

import javax.enterprise.context.ApplicationScoped;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class TeacherServiceImpl
    implements TeacherService
{

    //添加老师
    @Override
    public void addTeacher(String name,String payrollNumber,String password,String path)
    {
        SqlSession sqlSession = SqlSessionGetter.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        mapper.insertTeacher(name,payrollNumber,password,path);

        sqlSession.close();
        return;

    }

    @Override
    public void deleteTeacher(String name, String payrollNumber)
    {
        SqlSession sqlSession = SqlSessionGetter.getSqlSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        mapper.deleteTeacher(name, payrollNumber);

        sqlSession.close();
        return;
    }

    //根据工资号及密码更新其余属性
    @Override
    public void flushTeacher(String payrollNumber, String password,Teacher newTeacher)
    {
        SqlSession sqlSession = SqlSessionGetter.getSqlSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        mapper.startTransaction();

        try
        {
            if (newTeacher.getName() != null && !"".equals(newTeacher.getName()))
            {
                mapper.updateNameByPassword(payrollNumber,password,newTeacher.getName());
            }
            if(newTeacher.getPassword() != null && !"".equals(newTeacher.getPassword()))
            {
                mapper.updatePasswordByPassword(payrollNumber,password,newTeacher.getPassword());
            }
        }
        catch (Exception e)
        {
            mapper.rollBack();
        }

        mapper.commit();
        sqlSession.close();
        return;
    }

    //根据姓名工资号更改
    @Override
    public void flushTeacherByName(String name, String payrollNumber,Teacher newTeacher)
    {
        SqlSession sqlSession = SqlSessionGetter.getSqlSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        mapper.startTransaction();

        try
        {
            if (newTeacher.getName() != null)
            {
                mapper.updateNameByName(payrollNumber,name,newTeacher.getName());
            }
            if(newTeacher.getPassword() != null)
            {
                mapper.updatePasswordByName(payrollNumber,newTeacher.getName(),newTeacher.getPassword());
                System.out.println(111);
            }
        }
        catch (Exception e)
        {
            mapper.rollBack();
        }
        mapper.commit();
        sqlSession.close();
        return;

    }

    @Override
    public void flushTeacherImg(Teacher teacher, Image image)
            throws IOException
    {
        File file = new File("src/main/resources/teacherPictures/" + imgName(teacher.getName()) + ".jpg");

        while (file.exists())
        {
            file = new File("src/main/resources/teacherPictures/" + imgName(teacher.getName()) + ".jpg");
        }
        file.createNewFile();

        //根据image获取一个bufferedimage
        BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bufferedImage.getGraphics();
        graphics.drawImage(image,0,0,image.getWidth(null),image.getHeight(null),null);

        //写图片
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ImageIO.write(bufferedImage,"jpg",fileOutputStream);
        fileOutputStream.close();

        //将保存的图片路径存入数据库
        teacher.setHeadPortraitPath(file.getPath());
        TeacherDao.updatePath(teacher.getPayrollNumber(),teacher.getPassword(),teacher.getHeadPortraitPath());

    }
    public String imgName(String name)
    {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy mm dd");
        String format = simpleDateFormat.format(date);

        //给照片起个名字
        String imgName = name;
        imgName += " " + format + " ";
        imgName += UUID.randomUUID().toString();
        return imgName;
    }

    @Override
    public Teacher loginTeacher(String payrollNumber, String password)
    {
        Teacher teacher = TeacherDao.selectTeacher(payrollNumber, password);
        return teacher;
    }

    @Override
    public String getTeachersJson(int start,int end)
    {
        SqlSession sqlSession = SqlSessionGetter.getSqlSession();

        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        int number = mapper.selectNumberOfTeachers();

        List<Teacher> teachers = mapper.selectTeachers(start, end - start + 1);

        AmountAndListTeach amountAndList = new AmountAndListTeach(number,teachers);

        String json = GetJsonUtil.getJson(amountAndList);

        sqlSession.close();
        return json;
    }


    public static void main(String[] args)
            throws IOException
    {
        Teacher teacher = new Teacher();
        teacher.setName("123");

        Image read = ImageIO.read(new File("C:\\Users\\31869\\Pictures\\Saved Pictures\\QQ图片20210704180631.png"));

        TeacherServiceImpl teacherService = new TeacherServiceImpl();
        teacherService.flushTeacherImg(teacher,read);
    }
}
